package com.kk.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kk.commonutils.R;
import com.kk.eduservice.entity.EduTeacher;
import com.kk.eduservice.entity.vo.TeacherQuery;
import com.kk.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-04
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 查询所有讲师
     * @return
     */
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 根据id逻辑删除讲师
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页查询所有讲师
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") long current,@PathVariable("limit") long limit){
            //创建page
            Page<EduTeacher> pageTeacher = new Page<>(current, limit);

            //调用方法，把所有数据封装到pageTeacher中
            teacherService.page(pageTeacher, null);

            long total = pageTeacher.getTotal();//获取总记录数
            List<EduTeacher> records = pageTeacher.getRecords();//获取分页后的list集合

            HashMap<String, Object> map = new HashMap<>();
            map.put("total", total);
            map.put("rows", records);
            return R.ok().data(map);
    }

    /**
     * 分页带条件查询所有讲师！
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current, limit);

        //  多条件组合查询
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }

        //  排序  根据添加讲师的时间进行排序
        queryWrapper.orderByDesc("gmt_create");

        teacherService.page(page,queryWrapper);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @PostMapping("/addTeacher")
    public R save(@RequestBody EduTeacher teacher){
        boolean b = teacherService.save(teacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据id查询讲师
     * @param id
     * @return
     */
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }



}

